<?php

class CategoryControl extends AuthControl{
	public function index(){
		$data = catched('category');
		$data = Data::tree($data,'cname');
		$this->assign('data',$data);
		$this->display();
		
	}
	public function add(){

		if(IS_POST){
			$model = K('Category');

			if($model->addCategory()){
				$this->success('添加成功',U('index'));
			}else{
				$this->error($model->error);
			}
		}else{
			$goodstype = catched('goodsType');
			$this->assign('goodstype',$goodstype);
			$this->display();
		}
		
	}
	public function save(){
		$cid = Q('cid',0,'intval');
		if(IS_POST){
			$model = K('Category');
			if($model->saveCategory()){
				$this->success('栏目更新成功',U('index'));
			}else{
				$this->error($model->error);
			}
		
		}else{
			
			//获得上级栏目数据
			$catdata = catched('category');
			//当前栏目信息
			$data = $catdata[$cid];
			// 判断是否是 自身或子栏目
			foreach ($catdata as $key => $c) {
				$catdata[$key]['selected']=$catdata[$key]['disabled'] = '';
				//使自己不能被选中
				if($cid == $c['cid']){
					$catdata[$key]['disabled'] = "disabled=''";

				}
				//使子栏目不能被选中
				if(Data::isChild($catdata,$c['cid'],$cid)){
					$catdata[$key]['disabled'] = "disabled=''";
				}
				//使父级栏目在编辑页面处于选中状态
				if($catdata[$cid]['pid']==$c['cid']){
					$catdata[$key]['selected'] = "selected='selected'";
				}
				
			}
			$catdata = Data::tree($catdata,'cname');
			// p($catdata );exit;
			$this->assign('catdata',$catdata);
			$this->assign('data',$data);
			$this->display();
		}
	}
	//缓存栏目
	public function updateCache(){
		$model = K('Category');
		if($model->updateCache()){
			$this->success('栏目缓存成功');
		}else{
			$this->error($model->error);
		}
	}
}
?>
